package com.stox.ui.javafx.core.view;

import java.util.LinkedList;
import java.util.List;

import javafx.animation.Transition;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

import com.stox.ui.javafx.core.animation.Transitions;
import com.stox.ui.javafx.core.util.ExceptionHandler.ExceptionHandlingRunnable;
import com.stox.ui.javafx.core.widget.StoxStage;

public class MessageView extends View{

	private static final List<MessageView> messageViews = new LinkedList<>();
	
	private static final double height = 100;
	private static final double width = 300;
	private static final double padding = 10;
	
	private long closeDelay = 3;
	private double transitionDelay = 3;
	
	private void setSize(){
		setResizableWindow(false);
		setPrefHeight(height);
		setPrefWidth(width);
	}
	
	private void setPosition(){
		if(messageViews.isEmpty()){
			Bounds paneBounds = StoxStage.getInstance().getContentPane().getBoundsInParent();
			relocate(paneBounds.getMaxX()-width - padding, paneBounds.getMaxY() - height - padding);
		}else{
			MessageView view = messageViews.get(0);
			relocate(view.getLayoutX(), view.getLayoutY() - padding - height);
		}
	}
	
	public void setMessage(String message, Color color){
		Text label = new Text(message);
		label.setFill(color);
		label.getStyleClass().add("message-text");

		label.setWrappingWidth(width);
		
		HBox.setHgrow(label, Priority.ALWAYS);
		VBox.setVgrow(label, Priority.ALWAYS);
		ScrollPane scrollPane = new ScrollPane(label);
		scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
		scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
		getContentPane().getChildren().add(scrollPane);
	}
	
	public void setError(String error){
		setMessage(error, Color.RED);
	}
	
	@Override
	public void open() {
		setSize();
		setPosition();
		StoxStage.getInstance().getRootPane().getChildren().add(this);
		
		super.open();
		messageViews.add(0, this);
		
		new Thread(){
			public void run() {
				try {
					sleep(closeDelay*1000);
				} catch (InterruptedException e) {
					
				}finally{
					Transition transition = Transitions.getTransition(MessageView.this, transitionDelay, 1, 1, 0, 0);
					transition.setOnFinished(new EventHandler<ActionEvent>(){
						@Override
						public void handle(ActionEvent event) {
							doClose();
						}
					});
					Platform.runLater(new ExceptionHandlingRunnable(){
						@Override
						public void doRun() {
							transition.play();
						}
					});
				}
			};
		}.start();
	}
	
	@Override
	public void close() {
		super.close();
		doClose();
	}
	
	private void doClose(){
		messageViews.remove(this);
		Platform.runLater(new ExceptionHandlingRunnable(){
			public void doRun(){
				StoxStage.getInstance().getRootPane().getChildren().remove(MessageView.this);
			}
		});
	}

	public long getCloseDelay() {
		return closeDelay;
	}

	public void setCloseDelay(long closeDelay) {
		this.closeDelay = closeDelay;
	}

	public double getTransitionDelay() {
		return transitionDelay;
	}

	public void setTransitionDelay(double transitionDelay) {
		this.transitionDelay = transitionDelay;
	}
}
